import configs from '@/common/config.js'
// #ifdef H5
import {
	wxAuthorize
} from '@/utils/wxauth.js'
import jwx from '@/utils/jwx.js'
// #endif

const oss = (url) => {
	if (!url) {
		return ''
	}
	if (typeof url === 'string') {
		if ((/^(http|https):\/\/.+/.test(url))) {
			return url
		} else if ((/^(data:image).+/.test(url))) {
			return configs.imgUrl + uni.getStorageSync('project').user_logo_image
		} else{
			return configs.imgUrl + url
		}
	} else {
		let arr = []
		for (let i = 0; i < url.length; i++) {
			if ((/^(http|https):\/\/.+/.test(url[i]))) {
				arr.push(url[i])
			} else {
				arr.push(configs.imgUrl + url[i])
			}
		}
		return arr
	}
}


const authTo = (url) => {
	// uni.navigateTo({
	// 	url: url,
	// });
	if (uni.getStorageSync('token')) {
		uni.navigateTo({
			url: url
		});
	} else {
		uni.navigateTo({
			url: configs.login
		});
	}
}

const login = ()=>{
	uni.navigateTo({
		url: configs.login
	});
}

const goTo = (url) => {
	if(url[0] !== '/'){
		url = '/'+url
	}
	uni.navigateTo({
		url: url,
		fail(e) {
			
		}
	});
}

const copy = (content) => {
	uni.setClipboardData({
		data: content,
		success: function() {
			uni.$u.toast('复制成功')
		},
		fail: function() {
			uni.$u.toast('复制失败')
		}
	});
}

const back = () => {
	let pages = getCurrentPages();
	if (pages.length > 1) {
		uni.navigateBack();
	} else {
		uni.reLaunch({
			url: '/pages/index/index'
		});
	}
}

const toast = (word) => {
	uni.$u.toast(word);
}




const pay = (data) => {
	return new Promise((resolve, reject) => {
		if (data.paytype == 0) {
			// 支付宝支付
			uni.requestPayment({
				provider: 'alipay',
				orderInfo: data.pay, //支付宝订单数据
				success: function(res) {
					resolve()
				},
				fail: function(err) {
					reject()
				}
			});
		} else if (data.paytype == 1) {
			// 微信支付APp
			let datb = JSON.parse(data.pay)
			uni.requestPayment({
				provider: 'wxpay',
				orderInfo: datb,
				success: (e) => {
					resolve()
				},
				fail: (e) => {
					reject(e)
				}
			})
		
		} else if (data.paytype == 2) {
			// 微信小程序
			uni.requestPayment({
				appId: data.pay.appId,
				nonceStr: data.pay.nonceStr,
				package: data.pay.package,
				paySign: data.pay.paySign,
				signType: data.pay.signType,
				timeStamp: data.pay.timeStamp,
				success: (e) => {
					resolve()
				},
				fail: (e) => {
					reject(e)
				}
			})
		} else if (data.paytype == 3) {
			// 微信支付公众号
			// #ifdef H5
			jwx.wxpay({
				data: data.pay,
				success: res => {
					resolve(res)
				},
				fail: err => {
					reject(err)
				}
			})
			// #endif
		
		} else if (data.paytype == 4) {
			// 余额支付
			resolve()
		}
	})
	
}



const utils = Vue => {
	Vue.prototype.$authTo = authTo
	Vue.prototype.$goTo = goTo
	Vue.prototype.$oss = oss
	Vue.prototype.$copy = copy
	Vue.prototype.$back = back
	Vue.prototype.$toast = toast
	Vue.prototype.$pay = pay
	Vue.prototype.$login = login
	Vue.prototype.$uploadUrl = configs.baseUrl + '/api/upload'
	

	// #ifdef H5
	Vue.prototype.$wxOauth = wxAuthorize
	Vue.prototype.$jwx = jwx
	// #endif

	// #ifndef H5
	Vue.prototype.$safeT = uni.getSystemInfoSync().statusBarHeight + 10;
	// #endif
	// #ifdef H5
	Vue.prototype.$safeT = uni.getSystemInfoSync().statusBarHeight + 20;
	// #endif
}


export default utils